
#include "mycoin.h"
#include<QTimer>

//MyCoin::MyCoin(QWidget *parent)
//    : QWidget{parent}
//{

//}
MyCoin::MyCoin(QString btnImg)
{
    QPixmap pix;
    pix.load(btnImg);
    this->setFixedSize(pix.width(),pix.height());
    this->setIcon(pix);
    this->setIconSize(QSize(pix.width(),pix.height()));
    //初始化定时器对象
    timer1=new QTimer(this);
    timer2=new QTimer(this);

    //监听正面翻反面的信号 翻转金币
    connect(timer1,&QTimer::timeout,[=](){
        QPixmap pix;
        QString str=QString(":/,/coin2/coin0%1").arg(this->min++);
        pix.load(str);
        this->setFixedSize(pix.width(),pix.height());
        this->setStyleSheet("QPushButton{border:0px;");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));
        if(this->min>this->max)
        {
            this->min=1;
            timer1->stop();
        }

    });
    //反面翻正面
    connect(timer2,&QTimer::timeout,[=](){
        QPixmap pix;
        QString str=QString(":/,/coin2/coin0%1").arg(this->max--);
        pix.load(str);
        this->setFixedSize(pix.width(),pix.height());
        this->setStyleSheet("QPushButton{border:0px;");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));
        if(this->max<this->min)
        {
            this->max=2;
            timer2->stop();
        }

    });
}
void MyCoin::mousePressEvent(QMouseEvent*e)
{
    if(this->isWin)
    {
        return;
    }
    else
    {
        QPushButton::mousePressEvent(e);
    }
}


void MyCoin::changeFlag()
{
    if(this->flag)
    {
        timer1->start(10);
        this->flag=false;
    }
    else
    {
        timer2->start(10);
        this->flag=true;
    }
}